<template>
	<div class="edit_view" :style='{}'>
        <div class="breadcrumb-wrapper" style="width: 100%;">
            <div class="bread_view">
                <el-breadcrumb separator="" class="breadcrumb">
                    <el-breadcrumb-item class="first_breadcrumb" :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item class="second_breadcrumb" v-for="(item,index) in breadList" :key="index">{{item.name}}</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
        </div>
		<el-form ref="formRef" :model="form" class="add_form" label-width="120px" :rules="rules">
			<el-row>
				<el-col :span="12">
					<el-form-item label="成果名称" prop="chengguomingcheng">
						<el-input class="list_inp" v-model="form.chengguomingcheng" placeholder="成果名称"
							 type="text" 							:readonly="!isAdd||disabledForm.chengguomingcheng?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="成果分类" prop="chengguofenlei">
						<el-select
							class="list_sel"
							:disabled="!isAdd||disabledForm.chengguofenlei?true:false"
							v-model="form.chengguofenlei" 
							placeholder="请选择成果分类"
							style="width:100%;"
							>
							<el-option v-for="(item,index) in chengguofenleiLists" :label="item"
								:value="item"
								>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="图片" prop="tupian">
						<uploads
							:disabled="!isAdd||disabledForm.tupian?true:false"
							action="file/upload" 
							tip="请上传图片"
							style="width: 100%;text-align: left;"
							:fileUrls="form.tupian?form.tupian:''" 
							@change="tupianUploadSuccess">
						</uploads>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="评定等级" prop="pingdingdengji">
						<el-select
							class="list_sel"
							:disabled="!isAdd||disabledForm.pingdingdengji?true:false"
							v-model="form.pingdingdengji" 
							placeholder="请选择评定等级"
							style="width:100%;"
							>
							<el-option v-for="(item,index) in pingdingdengjiLists" :label="item"
								:value="item"
								>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="所属行业" prop="suoshuxingye">
						<el-input class="list_inp" v-model="form.suoshuxingye" placeholder="所属行业"
							 type="text" 							:readonly="!isAdd||disabledForm.suoshuxingye?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="获得时间" prop="huodeshijian">
						<el-date-picker
							class="list_date"
							v-model="form.huodeshijian"
							format="YYYY-MM-DD HH:mm:ss"
							value-format="YYYY-MM-DD HH:mm:ss"
							type="datetime"
							style="width:100%;"
							:readonly="!isAdd||disabledForm.huodeshijian?true:false"
							placeholder="请选择获得时间" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="城市" prop="chengshi">
						<el-input class="list_inp" v-model="form.chengshi" placeholder="城市"
							 type="text" 							:readonly="!isAdd||disabledForm.chengshi?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="县区" prop="xianqu">
						<el-input class="list_inp" v-model="form.xianqu" placeholder="县区"
							 type="text" 							:readonly="!isAdd||disabledForm.xianqu?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="街道" prop="jiedao">
						<el-input class="list_inp" v-model="form.jiedao" placeholder="街道"
							 type="text" 							:readonly="!isAdd||disabledForm.jiedao?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="学校名称" prop="xuexiaomingcheng">
						<el-input class="list_inp" v-model="form.xuexiaomingcheng" placeholder="学校名称"
							 type="text" 							:readonly="!isAdd||disabledForm.xuexiaomingcheng?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="学院名称" prop="xueyuanmingcheng">
						<el-input class="list_inp" v-model="form.xueyuanmingcheng" placeholder="学院名称"
							 type="text" 							:readonly="!isAdd||disabledForm.xueyuanmingcheng?true:false" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="简介" prop="jianjie">
						<el-input v-model="form.jianjie" placeholder="简介" type="textarea"
						:readonly="!isAdd||disabledForm.jianjie?true:false"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="应用场景" prop="yingyongchangjing">
						<el-input v-model="form.yingyongchangjing" placeholder="应用场景" type="textarea"
						:readonly="!isAdd||disabledForm.yingyongchangjing?true:false"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="技术实现" prop="jishushixian">
						<el-input v-model="form.jishushixian" placeholder="技术实现" type="textarea"
						:readonly="!isAdd||disabledForm.jishushixian?true:false"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="成果详情" prop="chengguoxiangqing">
						<editor class="list_editor" :value="form.chengguoxiangqing" placeholder="请输入成果详情" :readonly="!isAdd||disabledForm.chengguoxiangqing?true:false"
							@change="(e)=>editorChange(e,'chengguoxiangqing')"></editor>
					</el-form-item>
				</el-col>
			</el-row>
			<div class="formModel_btn_box">
				<el-button class="formModel_cancel" @click="backClick">取消</el-button>
				<el-button class="formModel_confirm"
                           @click="save"
                           type="success"
				>
					申请
				</el-button>
			</div>
		</el-form>
	</div>
</template>
<script setup>
	import {
		ref,
		getCurrentInstance,
		watch,
		onUnmounted,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		useRoute,
		useRouter
	} from 'vue-router';
	const context = getCurrentInstance()?.appContext.config.globalProperties;
	const route = useRoute()
	const router = useRouter()
	//基础信息
	const tableName = 'xiangmuchengguo'
	const formName = '项目成果'
	//基础信息
	const breadList = ref([{
		name: formName
	}])
	//获取唯一标识
	const getUUID =()=> {
      return new Date().getTime();
    }
	//form表单
	const form = ref({
		chengguomingcheng: '',
		chengguofenlei: '',
		tupian: '',
		jianjie: '',
		pingdingdengji: '',
		suoshuxingye: '',
		yingyongchangjing: '',
		jishushixian: '',
		chengguoxiangqing: '',
		huodeshijian: '',
		sfsh: '待审核',
		shhf: '',
		chengshi: '',
		xianqu: '',
		jiedao: '',
		xuexiaomingcheng: '',
		xueyuanmingcheng: '',
		userid: '',
	})
	const formRef = ref(null)
	const id = ref(0)
	const type = ref('')
	const disabledForm = ref({
		chengguomingcheng : false,
		chengguofenlei : false,
		tupian : false,
		jianjie : false,
		pingdingdengji : false,
		suoshuxingye : false,
		yingyongchangjing : false,
		jishushixian : false,
		chengguoxiangqing : false,
		huodeshijian : false,
		sfsh : false,
		shhf : false,
		chengshi : false,
		xianqu : false,
		jiedao : false,
		xuexiaomingcheng : false,
		xueyuanmingcheng : false,
		userid : false,
	})
	const isAdd = ref(false)
	//表单验证
	//匹配整数
	const validateIntNumber = (rule, value, callback) => {
		if (!value) {
			callback();
		} else if (!context?.$toolUtil.isIntNumer(value)) {
			callback(new Error("请输入整数"));
		} else {
			callback();
		}
	}
	//匹配数字
	const validateNumber = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isNumber(value)) {
			callback(new Error("请输入数字"));
		} else {
			callback();
		}
	}
	//匹配手机号码
	const validateMobile = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isMobile(value)) {
			callback(new Error("请输入正确的手机号码"));
		} else {
			callback();
		}
	}
	//匹配电话号码
	const validatePhone = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isPhone(value)) {
			callback(new Error("请输入正确的电话号码"));
		} else {
			callback();
		}
	}
	//匹配邮箱
	const validateEmail = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isEmail(value)) {
			callback(new Error("请输入正确的邮箱地址"));
		} else {
			callback();
		}
	}
	//匹配身份证
	const validateIdCard = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.checkIdCard(value)) {
			callback(new Error("请输入正确的身份证号码"));
		} else {
			callback();
		}
	}
	//匹配网站地址
	const validateUrl = (rule, value, callback) => {
		if(!value){
			callback();
		} else if (!context?.$toolUtil.isURL(value)) {
			callback(new Error("请输入正确的URL地址"));
		} else {
			callback();
		}
	}
	const rules = ref({
		chengguomingcheng: [
			{required: true,message: '请输入',trigger: 'blur'}, 
		],
		chengguofenlei: [
			{required: true,message: '请输入',trigger: 'blur'}, 
		],
		tupian: [
		],
		jianjie: [
		],
		pingdingdengji: [
			{required: true,message: '请输入',trigger: 'blur'}, 
		],
		suoshuxingye: [
		],
		yingyongchangjing: [
		],
		jishushixian: [
		],
		chengguoxiangqing: [
		],
		huodeshijian: [
		],
		sfsh: [
		],
		shhf: [
		],
		chengshi: [
		],
		xianqu: [
		],
		jiedao: [
		],
		xuexiaomingcheng: [
		],
		xueyuanmingcheng: [
		],
		userid: [
			{required: true,message: '请输入',trigger: 'blur'}, 
		],
	})
	//成果分类列表
	const chengguofenleiLists = ref([])
	//图片上传回调
	const tupianUploadSuccess=(e)=>{
		form.value.tupian = e
	}
	//评定等级列表
	const pingdingdengjiLists = ref([])
	//methods

	//methods
	//获取info
	const getInfo = ()=>{
		context?.$http({
			url: `${tableName}/info/${id.value}`,
			method: 'get'
		}).then(res => {
			let reg=new RegExp('../../../file','g')
			res.data.data.chengguoxiangqing = res.data.data.chengguoxiangqing.replace(reg,'../../../cl29556703/file');
			form.value = res.data.data
		})
	}
	const crossRow = ref('')
	const crossTable = ref('')
	const crossTips = ref('')
	const crossColumnName = ref('')
	const crossColumnValue = ref('')
	//初始化
	const init = (formId=null,formType='add',formNames='',row=null,table=null,statusColumnName=null,tips=null,statusColumnValue=null) => {
			form.value.huodeshijian = context?.$toolUtil.getCurDateTime()
		if(formId){
			id.value = formId
			type.value = formType
		}
		if(formType == 'add'){
			isAdd.value = true
		}else if(formType == 'info'){
			isAdd.value = false
			getInfo()
		}else if(formType == 'edit'){
			isAdd.value = true
			getInfo()
		}
		else if(formType == 'cross'){
			isAdd.value = true
			// getInfo()
			for(let x in row){
				if(x=='chengguomingcheng'){
					form.value.chengguomingcheng = row[x];
					disabledForm.value.chengguomingcheng = true;
					continue;
				}
				if(x=='chengguofenlei'){
					form.value.chengguofenlei = row[x];
					disabledForm.value.chengguofenlei = true;
					continue;
				}
				if(x=='tupian'){
					form.value.tupian = row[x];
					disabledForm.value.tupian = true;
					continue;
				}
				if(x=='jianjie'){
					form.value.jianjie = row[x];
					disabledForm.value.jianjie = true;
					continue;
				}
				if(x=='pingdingdengji'){
					form.value.pingdingdengji = row[x];
					disabledForm.value.pingdingdengji = true;
					continue;
				}
				if(x=='suoshuxingye'){
					form.value.suoshuxingye = row[x];
					disabledForm.value.suoshuxingye = true;
					continue;
				}
				if(x=='yingyongchangjing'){
					form.value.yingyongchangjing = row[x];
					disabledForm.value.yingyongchangjing = true;
					continue;
				}
				if(x=='jishushixian'){
					form.value.jishushixian = row[x];
					disabledForm.value.jishushixian = true;
					continue;
				}
				if(x=='chengguoxiangqing'){
					form.value.chengguoxiangqing = row[x];
					disabledForm.value.chengguoxiangqing = true;
					continue;
				}
				if(x=='huodeshijian'){
					form.value.huodeshijian = row[x];
					disabledForm.value.huodeshijian = true;
					continue;
				}
				if(x=='storeupNumber'){
					form.value.storeupNumber = row[x];
					disabledForm.value.storeupNumber = true;
					continue;
				}
				if(x=='clickNumber'){
					form.value.clickNumber = row[x];
					disabledForm.value.clickNumber = true;
					continue;
				}
				if(x=='thumbsupNumber'){
					form.value.thumbsupNumber = row[x];
					disabledForm.value.thumbsupNumber = true;
					continue;
				}
				if(x=='crazilyNumber'){
					form.value.crazilyNumber = row[x];
					disabledForm.value.crazilyNumber = true;
					continue;
				}
				if(x=='discussNumber'){
					form.value.discussNumber = row[x];
					disabledForm.value.discussNumber = true;
					continue;
				}
				if(x=='xuehao'){
					form.value.xuehao = row[x];
					disabledForm.value.xuehao = true;
					continue;
				}
				if(x=='xueshengxingming'){
					form.value.xueshengxingming = row[x];
					disabledForm.value.xueshengxingming = true;
					continue;
				}
				if(x=='fuzerenzhanghao'){
					form.value.fuzerenzhanghao = row[x];
					disabledForm.value.fuzerenzhanghao = true;
					continue;
				}
				if(x=='fuzerenxingming'){
					form.value.fuzerenxingming = row[x];
					disabledForm.value.fuzerenxingming = true;
					continue;
				}
				if(x=='daoshigonghao'){
					form.value.daoshigonghao = row[x];
					disabledForm.value.daoshigonghao = true;
					continue;
				}
				if(x=='chengshi'){
					form.value.chengshi = row[x];
					disabledForm.value.chengshi = true;
					continue;
				}
				if(x=='xianqu'){
					form.value.xianqu = row[x];
					disabledForm.value.xianqu = true;
					continue;
				}
				if(x=='jiedao'){
					form.value.jiedao = row[x];
					disabledForm.value.jiedao = true;
					continue;
				}
				if(x=='xuexiaomingcheng'){
					form.value.xuexiaomingcheng = row[x];
					disabledForm.value.xuexiaomingcheng = true;
					continue;
				}
				if(x=='xueyuanmingcheng'){
					form.value.xueyuanmingcheng = row[x];
					disabledForm.value.xueyuanmingcheng = true;
					continue;
				}
				if(x=='userid'){
					form.value.userid = row[x];
					disabledForm.value.userid = true;
					continue;
				}
			}
			if(row){
				crossRow.value = row
			}
			if(table){
				crossTable.value = table
			}
			if(tips){
				crossTips.value = tips
			}
			if(statusColumnName){
				crossColumnName.value = statusColumnName
			}
			if(statusColumnValue){
				crossColumnValue.value = statusColumnValue
			}
			form.value.sfsh='待审核'
			form.value.storeupNumber='0'
			form.value.clickNumber='0'
		}
		context?.$http({
			url: `${context?.$toolUtil.storageGet('frontSessionTable')}/session`,
			method: 'get'
		}).then(res => {
			var json = res.data.data
            if (localStorage.getItem('autoSave')) {
                localStorage.removeItem('autoSave')
                save()
            }
		})
		context?.$http({
			url: `option/chengguofenlei/chengguofenlei`,
			method: 'get'
		}).then(res=>{
			chengguofenleiLists.value = res.data.data
		})
		pingdingdengjiLists.value = "优秀,良好,待提升".split(',')
	}
	//初始化
	//取消
	const backClick = () => {
		history.back()
	}
	//富文本数据回调
	const editorChange = (e,name) =>{
		form.value[name] = e
	}
	//提交
	const save=()=>{
		form.value.userid = context?.$toolUtil.storageGet('userid')
		if(form.value.tupian!=null) {
			form.value.tupian = form.value.tupian.replace(new RegExp(context?.$config.url,"g"),"");
		}
		var table = crossTable.value
		var objcross = JSON.parse(JSON.stringify(crossRow.value))
		let crossUserId = ''
		let crossRefId = ''
		let crossOptNum = ''
		if(type.value == 'cross'){
			if(crossColumnName.value!=''){
				if(!crossColumnName.value.startsWith('[')){
					for(let o in objcross){
						if(o == crossColumnName.value){
							objcross[o] = crossColumnValue.value
						}
					}
					//修改跨表数据
					changeCrossData(objcross)
				}else{
					crossUserId = context?.$toolUtil.storageGet('userid')
					crossRefId = objcross['id']
					crossOptNum = crossColumnName.value.replace(/\[/,"").replace(/\]/,"")
				}
			}
		}
		formRef.value.validate((valid)=>{
			if(valid){
				if(crossUserId&&crossRefId){
					form.value.crossuserid = crossUserId
					form.value.crossrefid = crossRefId
					let params = {
						page: 1,
						limit: 1000, 
						crossuserid:form.value.crossuserid,
						crossrefid:form.value.crossrefid,
					}
					context?.$http({
						url: `${tableName}/page`,
						method: 'get', 
						params: params 
					}).then(res=>{
						if(res.data.data.total>=crossOptNum){
							context?.$toolUtil.message(`${crossTips.value}`,'error')
							return false
						}else{
							context?.$http({
								url: `${tableName}/${!form.value.id ? "save" : "update"}`,
								method: 'post', 
								data: form.value 
							}).then(res=>{
                                context?.$toolUtil.message(`操作成功`,'success')
                                history.back()
							})
						}
					})
				}else{
					context?.$http({
						url: `${tableName}/${!form.value.id ? "save" : "update"}`,
						method: 'post', 
						data: form.value 
					}).then(res=>{
                        context?.$toolUtil.message(`操作成功`,'success')
                        history.back()
					})
				}
			}
		})
	}
	//修改跨表数据
	const changeCrossData=(row)=>{
        if(type.value == 'cross'){
            context?.$http({
                url: `${crossTable.value}/update`,
                method: 'post',
                data: row
            }).then(res=>{})
        }
	}
	onMounted(()=>{
		type.value = route.query.type?route.query.type:'add'
		let row = null
		let table = null
		let statusColumnName = null
		let tips = null
		let statusColumnValue = null
		if(type.value == 'cross'){
			row = context?.$toolUtil.storageGet('crossObj')?JSON.parse(context?.$toolUtil.storageGet('crossObj')):{}
			table = context?.$toolUtil.storageGet('crossTable')
			statusColumnName = context?.$toolUtil.storageGet('crossStatusColumnName')
			tips = context?.$toolUtil.storageGet('crossTips')
			statusColumnValue = context?.$toolUtil.storageGet('crossStatusColumnValue')
		}
		init(route.query.id?route.query.id:null, type.value,'', row, table, statusColumnName, tips, statusColumnValue)
	})
    onUnmounted(()=>{
        Object.keys(localStorage).map(item=>{
            if(item.startsWith('cross')){
                localStorage.removeItem(item)
            }
        })
    })
</script>
<style lang="scss" scoped>
	// 面包屑盒子
	.bread_view {
		:deep(.breadcrumb) {
			.el-breadcrumb__separator {
			}
			.first_breadcrumb {
				.el-breadcrumb__inner {
				}
			}
			.second_breadcrumb {
				.el-breadcrumb__inner {
				}
			}
		}
	}
	// 表单
	.add_form{
		// form item
		:deep(.el-form-item) {
			//label
			.el-form-item__label {
			}
			// 内容盒子
			.el-form-item__content {
				// 输入框
				.list_inp {

				}
				//日期选择器
				.list_date {
				}
				// 下拉框
				.list_sel {
					//去掉默认样式
					.select-trigger{
						height: 100%;
						.el-input{
							height: 100%;
						}
					}
				}
				// 富文本
				.list_editor {
				}
				// 长文本
				.el-textarea__inner {
				}
				//图片上传样式
				.el-upload-list  {
					//提示语
					.el-upload__tip {
					}
					//外部盒子
					.el-upload--picture-card {
						//图标
						.el-icon{
						}
					}
					.el-upload-list__item {
					}
				}
			}
		}
	}
	// 按钮盒子
	.formModel_btn_box {
		.formModel_cancel {
		}
		.formModel_cancel:hover {
		}
		
		.formModel_confirm {
		}
		.formModel_confirm:hover {
		}
	}
</style>
<style>
.edit_view {
    margin: 20px auto;
    width: 1400px;
    position: relative;
    background: rgb(255, 255, 255);
    font-size:16px;
    color:#666;
}
.edit_view .add_form{
    width: 100%;
    padding: 30px;
    border:1px solid #eee;
}
.edit_view .add_form .el-form-item{
    margin: 0px 0px 20px;
    display: flex;
}
.edit_view .add_form .el-form-item .el-form-item__label{
    width: 150px;
    background: none;
    text-align: right;
    display: block;
    font-size: 16px;
    color: rgb(51, 51, 51);
    font-weight: 500;
}
.edit_view .add_form .el-form-item .el-form-item__content{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: wrap;
    width: calc(100% - 120px);
}
.edit_view .add_form .el-form-item .el-form-item__content .list_inp{
    height: 36px;
    line-height: 36px;
    border: 1px solid rgba(226, 227, 229, 1);
    padding: 0px 10px;
    width: 100%;
    box-sizing: border-box;
    background: rgb(255, 255, 255);
    font-size: 16px;
}

.edit_view .add_form .el-form-item .el-form-item__content .list_sel{
    line-height: 36px;
    
    box-sizing: border-box;
    width: calc(100% - 120px);
    padding: 0px 10px;
    border-radius: 0px;
    background: rgb(255, 255, 255);
    font-size: 16px;
}

.edit_view .add_form .el-form-item .el-form-item__content .list_date{
    line-height: 36px;
    border: 1px solid rgba(226, 227, 229, 1);
    box-sizing: border-box;
    width: 100%;
    border-radius: 0px;
    background: rgb(255, 255, 255);
    font-size: 16px;
}




.edit_view .add_form .el-form-item .el-form-item__content .el-textarea{border: 1px solid rgba(226, 227, 229, 1);}
.edit_view .add_form .el-form-item .el-form-item__content .el-textarea .el-textarea__inner{
    width: 100%;
    min-height: 150px;
    padding: 12px;
    border: 1px solid rgba(255, 255, 255, 1);
    border-radius: 0px;
    color: #666;
    font-size: 16px;
}


.edit_view .add_form .el-form-item .el-form-item__content .el-upload--picture-card{
    background-color: rgb(255, 255, 255);
    width: 100px;
    height: 90px;
    line-height: 100px;
    text-align: center;
    
    border-radius: 0px;
    cursor: pointer;
}

.edit_view .add_form .el-form-item .el-form-item__content .el-upload--picture-card .el-icon{
    font-size: 32px;
    color: #999;
}

.edit_view .add_form .el-form-item .el-form-item__content .img-uploader .el-upload__tip{
    font-size: 15px;
    color: #666;
    margin: 0;
}


.edit_view .add_form .el-form-item .el-form-item__content .list_editor{
    width: 100%;
    height: auto;
    margin: 0px;
    padding: 0px;
    border-radius: 0px;
    border-width: 0px;
    border-style: solid;
    border-color: var(--theme);
    background-color: rgb(255, 255, 255);
}

</style>